package com.boful.mooc.search.provider;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.boful.mooc.search.exception.IndexException;
import com.boful.mooc.search.model.CourseModel;
import com.boful.mooc.search.model.GobalModel;
import com.boful.mooc.search.model.OrgModel;
import com.boful.mooc.search.model.QuestionModel;
import com.boful.mooc.search.model.TeacherModel;

public class MoocSearchProvider {
	private String gobalUrl;
	private String courseUrl;
	private String orgUrl;
	private String teacherUrl;
	private String askUrl;
	private HttpSolrServer gobalSolrServer;
	private HttpSolrServer courseSolrServer;
	private HttpSolrServer orgSolrServer;
	private HttpSolrServer teacherSolrServer;
	private HttpSolrServer askSolrServer;
	private static Logger logger = LoggerFactory
			.getLogger(MoocSearchProvider.class);

	public MoocSearchProvider(String gobalUrl, String courseUrl, String orgUrl,
			String teacherUrl, String askUrl) {
		super();
		this.gobalUrl = gobalUrl;
		this.courseUrl = courseUrl;
		this.orgUrl = orgUrl;
		this.teacherUrl = teacherUrl;
		this.askUrl = askUrl;
		initSolrServer();
	}

	private void initSolrServer() {
		gobalSolrServer = new HttpSolrServer(gobalUrl);
		gobalSolrServer.setSoTimeout(3000); // socket read timeout
		gobalSolrServer.setConnectionTimeout(1000);
		gobalSolrServer.setDefaultMaxConnectionsPerHost(1000);
		gobalSolrServer.setMaxTotalConnections(10);
		gobalSolrServer.setFollowRedirects(false); // defaults to false
		gobalSolrServer.setAllowCompression(true);
		gobalSolrServer.setMaxRetries(1);

		courseSolrServer = new HttpSolrServer(courseUrl);
		courseSolrServer.setSoTimeout(3000); // socket read timeout
		courseSolrServer.setConnectionTimeout(1000);
		courseSolrServer.setDefaultMaxConnectionsPerHost(1000);
		courseSolrServer.setMaxTotalConnections(10);
		courseSolrServer.setFollowRedirects(false); // defaults to false
		courseSolrServer.setAllowCompression(true);
		courseSolrServer.setMaxRetries(1);

		orgSolrServer = new HttpSolrServer(orgUrl);
		orgSolrServer.setSoTimeout(3000); // socket read timeout
		orgSolrServer.setConnectionTimeout(1000);
		orgSolrServer.setDefaultMaxConnectionsPerHost(1000);
		orgSolrServer.setMaxTotalConnections(10);
		orgSolrServer.setFollowRedirects(false); // defaults to false
		orgSolrServer.setAllowCompression(true);
		orgSolrServer.setMaxRetries(1);

		teacherSolrServer = new HttpSolrServer(teacherUrl);
		teacherSolrServer.setSoTimeout(3000); // socket read timeout
		teacherSolrServer.setConnectionTimeout(1000);
		teacherSolrServer.setDefaultMaxConnectionsPerHost(1000);
		teacherSolrServer.setMaxTotalConnections(10);
		teacherSolrServer.setFollowRedirects(false); // defaults to false
		teacherSolrServer.setAllowCompression(true);
		teacherSolrServer.setMaxRetries(1);

		askSolrServer = new HttpSolrServer(askUrl);
		askSolrServer.setSoTimeout(3000); // socket read timeout
		askSolrServer.setConnectionTimeout(1000);
		askSolrServer.setDefaultMaxConnectionsPerHost(1000);
		askSolrServer.setMaxTotalConnections(10);
		askSolrServer.setFollowRedirects(false); // defaults to false
		askSolrServer.setAllowCompression(true);
		askSolrServer.setMaxRetries(1);

	}

	public void addIndex(List<Object> objectList) throws IndexException {
		Class class1 = objectList.get(0).getClass();
		String name = class1.getName();
		try {
			if (name.equals("com.boful.mooc.course.domains.Course")) {

				addCourseIndex(objectList);

			} else if (name.equals("com.boful.mooc.user.domains.User")) {
				addTeacherIndex(objectList);
			} else if (name
					.equals("com.boful.mooc.organization.domains.Organization")) {
				addOrgIndex(objectList);
			} else if (name.equals("com.boful.mooc.ask.domains.Question")) {
				addQuestionIndex(objectList);
			} else {
				throw new IndexException("索引对象不在索引范围之内:" + name);

			}
		} catch (NoSuchFieldException | SecurityException
				| IllegalArgumentException | IllegalAccessException
				| NoSuchMethodException | InvocationTargetException e) {
			logger.error(e.getMessage(), e);
			throw new IndexException("属性不一致:" + name + ":" + e.getMessage());
		}
	}

	public void deleteIndex(String guid, Class c) {
		HttpSolrServer server = judeSolrServer(c);
		if (server != null) {
			try {
				server.deleteByQuery("guid:" + guid);
				server.commit();
			} catch (SolrServerException | IOException e) {
				e.printStackTrace();
			}

		}
	}

	public void updateIndex(Object object) throws IndexException {
		Class class1 = object.getClass();
		try {
			String guid = getFieldValue(object, "guid") + "";
			deleteIndex(guid, class1);
			List<Object> objects = new ArrayList<Object>();
			objects.add(object);
			addIndex(objects);
		} catch (NoSuchFieldException | SecurityException
				| IllegalArgumentException | IllegalAccessException
				| NoSuchMethodException | InvocationTargetException e) {
			throw new IndexException(e.getMessage());
		}
	}

	/***
	 * 搜索对象
	 * 
	 * @param class1
	 *            要搜索的模型
	 * @param fieldList
	 *            字段集合
	 * @param key
	 *            字段值
	 * @param relation
	 *            字段关系
	 * @param start
	 *            分页开始
	 * @param count
	 *            每页的数量
	 * @param sortfield
	 *            要排序的字段
	 * @param flag
	 *            升降序
	 * @param hightlight
	 *            是否高亮
	 * @return [total,modelList=[model,model,model],useTime]
	 */
	public Map<String, Object> searchObject(Class class1,
			List<String> fieldList, List<String> key, List<String> relation,
			List<String> facetedQueryList, int start, int count,
			List<String> sortfield, List<Boolean> flag, boolean hightlight) {
		// 检测输入是否合法
		if (null == fieldList || null == key || fieldList.size() != key.size()
				|| (relation.size() != fieldList.size() - 1)) {
			return null;
		}
		if (null == sortfield || null == flag
				|| sortfield.size() != flag.size()) {
			return null;
		}

		SolrQuery query = null;
		try {
			String params = "";
			for (int i = 0; i < fieldList.size(); i++) {
				if (i == fieldList.size() - 1) {
					params += fieldList.get(i) + ":" + key.get(i);
				} else {
					params += fieldList.get(i) + ":" + key.get(i) + " "
							+ relation.get(i) + " ";
				}

			}
			// 初始化查询对象
			query = new SolrQuery(params);
			if (facetedQueryList != null) {
				for (String facetedQuery : facetedQueryList) {
					query.addFacetQuery(facetedQuery);
				}

			}
			// 设置起始位置与返回结果数
			query.setStart(start);
			query.setRows(count);
			// 设置排序
			for (int i = 0; i < sortfield.size(); i++) {
				if (flag.get(i)) {
					SolrQuery.SortClause sortClause = new SolrQuery.SortClause(
							sortfield.get(i), SolrQuery.ORDER.asc);
					query.addSort(sortClause);
				} else {
					SolrQuery.SortClause sortClause = new SolrQuery.SortClause(
							sortfield.get(i), SolrQuery.ORDER.desc);
					query.addSort(sortClause);
				}
			}
			// 设置高亮
			if (hightlight) {
				query.setHighlight(true); // 开启高亮组件
				for (String highLightField : fieldList) {
					query.addHighlightField(highLightField);// 高亮字段
				}
				query.setHighlightSimplePre("<font color=\"red\">");// 标记
				query.setHighlightSimplePost("</font>");
				query.setHighlightSnippets(1);// 结果分片数，默认为1
				query.setHighlightFragsize(300);// 每个分片的最大长度，默认为100
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		QueryResponse rsp = null;
		try {
			HttpSolrServer server = judeSolrServer(class1);
			rsp = server.query(query);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		// 返回查询结果
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", 0);
		if (rsp != null) {
			result.put("total", rsp.getResults().getNumFound());
			result.put("useTime", rsp.getElapsedTime() / 1000.00);
			List<?> modelList = rsp.getBeans(class1);
			result.put("modelList", modelList);
			Map<String, Map<String, List<String>>> hightlight2 = rsp
					.getHighlighting();
			for (int i = 0; i < modelList.size(); i++) {
				Object searchModel = modelList.get(i);
				// hightlight的键为Item的id，值唯一，我们设置的高亮字段为title
				if (hightlight2 != null && hightlight2.size() > 0) {
					try {

						String guid = getFieldValue(searchModel, "guid") + "";
						Map<String, List<String>> highLightObject = hightlight2
								.get(guid);
						for (String field : fieldList) {
							String highValue = highLightObject.get(field)
									.get(0);
							Method method = searchModel
									.getClass()
									.getDeclaredMethod(
											"set"
													+ Character.toUpperCase(field
															.charAt(0))
													+ field.substring(1),
											String.class);
							if (method != null) {
								method.invoke(searchModel, highValue);
							}
						}
					} catch (Exception e) {
					}
				}
			}
		}
		return result;
	}

	public String[] suggest(Class modelClass, String prefix, int min) {
		String[] words = null;
		StringBuffer sb = new StringBuffer("");
		SolrQuery query = new SolrQuery("*.*");
		QueryResponse rsp = new QueryResponse();
		// Facet为solr中的层次分类查询
		HttpSolrServer server = judeSolrServer(modelClass);
		try {
			query.setFacet(true);
			query.setQuery("*:*");
			query.setFacetPrefix(prefix);
			query.addFacetField("name");
			rsp = server.query(query);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}

		if (null != rsp) {
			FacetField ff = rsp.getFacetField("name");
			List<FacetField.Count> countList = ff.getValues();
			if (null == countList) {
				return null;
			}
			for (int i = 0; i < countList.size(); i++) {
				String[] tmp = countList.get(i).toString().split(" ");
				// 排除单个字
				if (tmp[0].length() < 2) {
					continue;
				}
				sb.append(tmp[0] + " ");
				min--;
				if (min == 0) {
					break;
				}
			}
			words = sb.toString().split(" ");
		} else {
			return null;
		}
		return words;
	}

	private HttpSolrServer judeSolrServer(Class c) {
		HttpSolrServer solrServer = null;
		if (c == CourseModel.class) {
			solrServer = courseSolrServer;
		} else if (c == TeacherModel.class) {
			solrServer = teacherSolrServer;
		} else if (c == GobalModel.class) {
			solrServer = gobalSolrServer;
		} else if (c == QuestionModel.class) {
			solrServer = askSolrServer;
		} else if (c == OrgModel.class) {
			solrServer = orgSolrServer;
		} else {
			return null;
		}
		return solrServer;
	}

	private Object searchObjectByGuid(String guid, Class c) {
		HttpSolrServer solrServer = judeSolrServer(c);
		if (solrServer == null) {
			return null;
		}
		SolrQuery query = new SolrQuery("guid:" + guid);
		try {
			QueryResponse response = solrServer.query(query);
			List models = response.getBeans(c);
			if (models != null && models.size() > 0) {
				return models.get(0);
			}
		} catch (SolrServerException e) {
			e.printStackTrace();
		}
		return null;

	}

	public CourseModel searchCourseByGuid(String guid) {
		Object object = searchObjectByGuid(guid, CourseModel.class);
		return object == null ? null : (CourseModel) object;
	}

	public GobalModel searchGobalByGuid(String guid) {
		Object object = searchObjectByGuid(guid, GobalModel.class);
		return object == null ? null : (GobalModel) object;
	}

	public OrgModel searchOrgByGuid(String guid) {
		Object object = searchObjectByGuid(guid, OrgModel.class);
		return object == null ? null : (OrgModel) object;
	}

	public QuestionModel searchQuestionByGuid(String guid) {
		Object object = searchObjectByGuid(guid, QuestionModel.class);
		return object == null ? null : (QuestionModel) object;
	}

	public TeacherModel searchTeacherByGuid(String guid) {
		Object object = searchObjectByGuid(guid, TeacherModel.class);
		return object == null ? null : (TeacherModel) object;
	}

	private void addQuestionIndex(List<Object> objectList)
			throws IndexException, NoSuchFieldException, SecurityException,
			IllegalArgumentException, IllegalAccessException,
			NoSuchMethodException, InvocationTargetException {
		Class questionClass = objectList.get(0).getClass();

		for (Object object : objectList) {
			QuestionModel questionModel = new QuestionModel();

			// createDate
			Date dateCreated = (Date) getFieldValue(object, "dateCreated");
			questionModel.setCreateDate(dateCreated);

			// creator
			Object creator = getFieldValue(object, "user");
			if (creator != null) {

				long creatorId = (long) getFieldValue(creator, "id");
				String creatorName = getFieldValue(creator, "name") + "";

				questionModel.setCreatorId(creatorId);
				questionModel.setCreatorName(creatorName);
			}

			// questionContent
			String questionContent = getFieldValue(object, "questionContent")
					+ "";
			questionModel.setDescription(questionContent);

			// guid
			String guid = getFieldValue(object, "guid") + "";

			questionModel.setGuid(guid);

			// id
			long id = (long) getFieldValue(object, "id");
			questionModel.setId(id);

			// title
			String title = getFieldValue(object, "title") + "";
			questionModel.setName(title);

			// rightAnswer
			Object rightAnswer = getFieldValue(object, "rightAnswer");
			if (rightAnswer == null) {
				questionModel.setState(QuestionModel.STATE_OPEN);
			} else {
				questionModel.setState(QuestionModel.STATE_CLOSED);
			}

			// category
			Object questionCategoryObject = getFieldValue(object,
					"questionCategory");
			if (questionCategoryObject != null) {
				long categoryId = (long) getFieldValue(questionCategoryObject,
						"id");
				String categoryName = getFieldValue(questionCategoryObject,
						"name") + "";
				questionModel.setCategoryId(categoryId);
				questionModel.setCategoryName(categoryName);
			}
			try {
				askSolrServer.addBean(questionModel);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SolrServerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		try {
			askSolrServer.optimize();
			gobalSolrServer.commit();
		} catch (IOException | SolrServerException e) {
			throw new IndexException("索引失败:" + e.getMessage());
		}

		addGobalIndex(objectList);

	}

	private void addGobalIndex(List<Object> objectList) throws IndexException,
			NoSuchFieldException, SecurityException, IllegalArgumentException,
			IllegalAccessException, NoSuchMethodException,
			InvocationTargetException {
		Class class1 = objectList.get(0).getClass();

		String name = class1.getName();

		for (Object object : objectList) {

			GobalModel gobalModel = new GobalModel();

			// guid
			String guid = getFieldValue(object, "guid") + "";

			gobalModel.setGuid(guid);

			if (searchGobalByGuid(guid) != null) {
				deleteIndex(guid, GobalModel.class);
			}

			// id
			long id = (long) getFieldValue(object, "id");
			gobalModel.setId(id);

			if (name.equals("com.boful.mooc.course.domains.Course")) {

				// description
				String description = (getFieldValue(object,
						"courseIntroduction") + "");
				gobalModel.setDescription(description);

				// name
				String objectName = getFieldValue(object, "name") + "";
				gobalModel.setName(objectName);

				// dataCreated
				Date dataCreated = (Date) getFieldValue(object, "dataCreated");
				gobalModel.setCreateDate(dataCreated);

				// creator
				Object creator = getFieldValue(object, "teacher");
				if (creator != null) {

					long creatorId = (long) getFieldValue(creator, "id");
					String creatorName = getFieldValue(creator, "email") + "";

					gobalModel.setCreatorId(creatorId);
					gobalModel.setCreatorName(creatorName);
				}

				gobalModel.setType(GobalModel.TYPE_COURSE);

			} else if (name.equals("com.boful.mooc.user.domains.User")) {
				// description
				String description = (getFieldValue(object, "description") + "");
				gobalModel.setDescription(description);

				// name
				String objectName = getFieldValue(object, "email") + "";
				gobalModel.setName(objectName);

				// dateCreated
				Date dateCreated = (Date) getFieldValue(object, "dateCreated");
				gobalModel.setCreateDate(dateCreated);
				gobalModel.setType(GobalModel.TYPE_TEACHER);
			} else if (name
					.equals("com.boful.mooc.organization.domains.Organization")) {
				// description
				String description = (getFieldValue(object, "description") + "");
				gobalModel.setDescription(description);

				// name
				String objectName = getFieldValue(object, "name") + "";
				gobalModel.setName(objectName);

				// createDate
				Date createDate = (Date) getFieldValue(object, "createDate");
				gobalModel.setCreateDate(createDate);
				gobalModel.setType(GobalModel.TYPE_ORG);

			} else if (name.equals("com.boful.mooc.ask.domains.Question")) {
				// createDate
				Date dateCreated = (Date) getFieldValue(object, "dateCreated");
				gobalModel.setCreateDate(dateCreated);

				// title
				String title = getFieldValue(object, "title") + "";
				gobalModel.setName(title);

				// questionContent
				String questionContent = getFieldValue(object,
						"questionContent") + "";
				gobalModel.setDescription(questionContent);

				gobalModel.setType(GobalModel.TYPE_ASK);
			} else {
				throw new IndexException("索引对象不在索引范围之内:" + name);
			}
			try {
				gobalSolrServer.addBean(gobalModel);
			} catch (IOException e) {
				e.printStackTrace();
			} catch (SolrServerException e) {
				e.printStackTrace();
			}
		}

		try {

			gobalSolrServer.optimize();
			gobalSolrServer.commit();
		} catch (IOException | SolrServerException e) {
			throw new IndexException("索引失败:" + e.getMessage());
		}
	}

	private Object getFieldValue(Object object, String fieldName)
			throws NoSuchFieldException, SecurityException,
			NoSuchMethodException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException {
		Field field = null;
		try {
			field = object.getClass().getDeclaredField(fieldName);
		} catch (NoSuchFieldException e) {
			field = object.getClass().getSuperclass()
					.getDeclaredField(fieldName);
		}

		String getMethodName = "";
		if (field.getGenericType() == boolean.class
				|| field.getGenericType() == Boolean.class) {
			getMethodName = "is";
		} else {
			getMethodName = "get";
		}
		getMethodName = getMethodName
				+ Character.toUpperCase(fieldName.charAt(0))
				+ fieldName.substring(1);
		Method method = object.getClass().getDeclaredMethod(getMethodName);
		return method.invoke(object);
	}

	private void addCourseIndex(List<Object> courseObjectList)
			throws IndexException, NoSuchFieldException, SecurityException,
			IllegalArgumentException, IllegalAccessException,
			NoSuchMethodException, InvocationTargetException {
		Class courseClass = courseObjectList.get(0).getClass();
		for (Object courseObject : courseObjectList) {
			CourseModel courseModel = new CourseModel();

			// category
			Object categoryObject = getFieldValue(courseObject, "category");
			if (categoryObject != null) {

				long categoryId = (long) getFieldValue(categoryObject, "id");
				String categoryName = getFieldValue(categoryObject, "name")
						+ "";

				courseModel.setCategoryId(categoryId);
				courseModel.setCategoryName(categoryName);
			}

			// createDate
			Date dataCreated = (Date) getFieldValue(courseObject, "dataCreated");
			courseModel.setCreateDate(dataCreated);

			// creator
			Object creator = getFieldValue(courseObject, "teacher");
			if (creator != null) {
				long creatorId = (long) getFieldValue(creator, "id");
				String creatorName = getFieldValue(creator, "email") + "";

				courseModel.setCreatorId(creatorId);
				courseModel.setCreatorName(creatorName);
			}

			// guid
			String guid = getFieldValue(courseObject, "guid") + "";
			courseModel.setGuid(guid);

			// id
			long id = (long) getFieldValue(courseObject, "id");
			courseModel.setId(id);

			// introduce
			String introduce = getFieldValue(courseObject, "courseIntroduction")
					+ "";
			courseModel.setIntroduce(introduce);

			// name
			String name = getFieldValue(courseObject, "name") + "";
			courseModel.setName(name);

			// startTime
			Date startTime = (Date) getFieldValue(courseObject, "startTime");
			courseModel.setOpenDate(startTime);

			// endTime
			Date endDate = (Date) getFieldValue(courseObject, "endTime");
			courseModel.setCloseDate(endDate);

			// littlePosterHash
			Object litterPoster = getFieldValue(courseObject, "litterPoster")
					+ "";
			if (litterPoster != null) {
				Object object = getFieldValue(litterPoster, "hash");
				if (object != null) {
					courseModel.setPhoto(object.toString());
				}
			}

			// price
			double price = (double) getFieldValue(courseObject, "price");
			courseModel.setPrice(price);
			try {
				courseSolrServer.addBean(courseModel);
			} catch (IOException | SolrServerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		try {

			courseSolrServer.optimize();
			courseSolrServer.commit();
		} catch (IOException | SolrServerException e) {
			throw new IndexException("索引失败:" + e.getMessage());
		}
		addGobalIndex(courseObjectList);

	}

	private void addOrgIndex(List<Object> orgObjectList) throws IndexException,
			NoSuchFieldException, SecurityException, IllegalArgumentException,
			IllegalAccessException, NoSuchMethodException,
			InvocationTargetException {
		Class orgClass = orgObjectList.get(0).getClass();

		for (Object orgObject : orgObjectList) {
			OrgModel orgModel = new OrgModel();

			// category
			Object categoryObject = getFieldValue(orgObject, "category");
			if (categoryObject != null) {
				long categoryId = (long) getFieldValue(categoryObject, "id");
				String categoryName = getFieldValue(categoryObject, "name")
						+ "";
				orgModel.setCategoryId(categoryId);
				orgModel.setCategoryName(categoryName);
			}

			// createDate
			Date createDate = (Date) getFieldValue(orgObject, "createDate");
			orgModel.setCreateDate(createDate);

			// description
			String description = getFieldValue(orgObject, "description") + "";
			orgModel.setDescription(description);

			// guid
			String guid = getFieldValue(orgObject, "guid") + "";
			orgModel.setGuid(guid);

			// icon
			String icon = getFieldValue(orgObject, "icon") + "";
			orgModel.setIcon(icon);

			// id
			long id = (long) getFieldValue(orgObject, "id");
			orgModel.setId(id);

			// name
			String name = getFieldValue(orgObject, "name") + "";
			orgModel.setName(name);
			try {
				orgSolrServer.addBean(orgModel);
			} catch (IOException | SolrServerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		try {

			orgSolrServer.optimize();
			orgSolrServer.commit();
		} catch (IOException | SolrServerException e) {
			throw new IndexException("索引失败:" + e.getMessage());
		}
		addGobalIndex(orgObjectList);

	}

	private void addTeacherIndex(List<Object> teacherObjectList)
			throws IndexException, NoSuchFieldException, SecurityException,
			IllegalArgumentException, IllegalAccessException,
			NoSuchMethodException, InvocationTargetException {
		Class teacherClass = teacherObjectList.get(0).getClass();

		for (Object teacherObject : teacherObjectList) {
			TeacherModel teacherModel = new TeacherModel();

			// dateCreated
			Date dateCreated = (Date) getFieldValue(teacherObject,
					"dateCreated");
			teacherModel.setCreateDate(dateCreated);

			// description
			String description = getFieldValue(teacherObject, "description")
					+ "";
			teacherModel.setDescription(description);

			// id
			long id = (long) getFieldValue(teacherObject, "id");
			teacherModel.setId(id);

			// introduce
			String introduce = getFieldValue(teacherObject, "introduce") + "";
			teacherModel.setIntroduce(introduce);

			// name
			String name = getFieldValue(teacherObject, "email") + "";
			teacherModel.setName(name);

			// photoPath
			String photoPath = getFieldValue(teacherObject, "photoPath") + "";
			teacherModel.setPhoto(photoPath);

			// sex
			boolean sex = (boolean) getFieldValue(teacherObject, "sex");
			teacherModel.setSex(sex);

			// uid
			String uid = getFieldValue(teacherObject, "guid") + "";
			teacherModel.setGuid(uid);
			try {
				teacherSolrServer.addBean(teacherModel);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SolrServerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		try {
			teacherSolrServer.optimize();
			teacherSolrServer.commit();
		} catch (IOException | SolrServerException e) {
			throw new IndexException("索引失败:" + e.getMessage());
		}
		addGobalIndex(teacherObjectList);

	}

	public MoocSearchProvider() {
		super();
	}

	public String getGobalUrl() {
		return gobalUrl;
	}

	public void setGobalUrl(String gobalUrl) {
		this.gobalUrl = gobalUrl;
	}

	public String getCourseUrl() {
		return courseUrl;
	}

	public void setCourseUrl(String courseUrl) {
		this.courseUrl = courseUrl;
	}

	public String getOrgUrl() {
		return orgUrl;
	}

	public void setOrgUrl(String orgUrl) {
		this.orgUrl = orgUrl;
	}

	public String getTeacherUrl() {
		return teacherUrl;
	}

	public void setTeacherUrl(String teacherUrl) {
		this.teacherUrl = teacherUrl;
	}

	public String getAskUrl() {
		return askUrl;
	}

	public void setAskUrl(String askUrl) {
		this.askUrl = askUrl;
	}

}
